package com.inews.business.web;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.Servlets;
import com.inews.business.entity.ProductType;
import com.inews.business.service.ProductTypeService;
import com.inews.business.util.BusinessContants;

@Controller
@RequestMapping(value = "/control/product")
public class ProductTypeController {
	
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@RequestMapping(value = "type/list")
	public String productTypeList(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request, 
			@RequestParam(value = "parentid", defaultValue = "0") int parentid, boolean query) {
		
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		if(!query){
			model.addAttribute("query", false);
			if(parentid > 0){
				ProductType parentProductType = productTypeService.findProductTypeById(Integer.valueOf(parentid));
				searchParams.put("EQ_parentType", parentProductType);
				model.addAttribute("parentid", parentid);
			}else{
				searchParams.put("ISNULL_parentType", null);
				model.addAttribute("parentid", 0);
			}
		}else{
			model.addAttribute("query", true);
			model.addAttribute("parentid", 0);
		}
		Page<ProductType> productTypes = productTypeService.getProductType(searchParams, pageNumber, BusinessContants.PAGE_SIZE, sortType);
		model.addAttribute("resData", ExtDataUtil.genWithPagingList(productTypes));
		model.addAttribute("sortType", sortType);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "product/producttypelist";
	}
	
	
	@RequestMapping(value = "type/addUI")
	public String addUI(Model model, @RequestParam(value = "parentid", defaultValue = "0") int parentid) {
		if(parentid > 0){
			model.addAttribute("parentid", parentid);
		}
		return "product/add_productType";
	}
	
	
	@RequestMapping(value = "type/add", method = RequestMethod.POST)
	public String add(Integer parentid, ProductType productType, Model model) {
		if(parentid != null && parentid > 0){
			productType.setParentType(productTypeService.findProductTypeById(parentid));
		}
		productTypeService.saveProductType(productType);
		model.addAttribute("message", "添加成功");
		model.addAttribute("urladdress", "/control/product/type/list");
		return "share/message";
	}
	
	
	@RequestMapping(value = "type/editUI")
	public String editUI(Model model, int typeid) {
		ProductType productType = productTypeService.findProductTypeById(typeid);
		model.addAttribute("productType", productType);
		return "product/edit_productType";
	}
	
	
	@RequestMapping(value = "type/edit", method = RequestMethod.POST)
	public String edit(ProductType productType, Model model) {
		if(productType != null){
			int typeid = productType.getTypeid();
			ProductType oriProdouctType = productTypeService.findProductTypeById(typeid);
			oriProdouctType.setName(productType.getName());
			oriProdouctType.setNote(productType.getNote());
			productTypeService.saveProductType(oriProdouctType);
		}
		model.addAttribute("message", "修改成功");
		model.addAttribute("urladdress", "/control/product/type/list");
		return "share/message";
	}
	
	
	@RequestMapping(value = "type/queryUI")
	public String queryUI() {
		return "product/query_productType";
	}

}
